import json

from selenium import webdriver
import time
import re




driver= ''
def open_browser(browser_name):
    global driver
    if browser_name.lower()=='chrome':
        driver= webdriver.Chrome('D:\\pythonglr\\python\\driver\\chromedriver.exe')
    elif browser_name.lower()=='ie':
        driver= webdriver.Chrome('D:\\pythonglr\\python\\driver\\chromedriver.exe')
    else:
        driver= webdriver.Chrome('D:\\pythonglr\\python\\driver\\chromedriver.exe')
def get(url):
    global driver
    driver.get(url)
def send(element_path,content):
    global driver
    try:
        driver.find_element_by_xpath(element_path).send_keys(content)
    except Exception as e:
        print(e)
def close():
    global driver
    driver.close()
    driver= ''
def click(element_path):
    global driver
    try:
        driver.find_element_by_xpath(element_path).click()
    except Exception as e:
        print(e)
def sleep(seconds):
    sends= int(seconds)
    time.sleep(sends)
def assert_word(key_word):
    global driver
    try:
        assert key_word in driver.page_source
    except Exception as e:
        print(e)

with open('data2.txt',encoding='utf-8') as fp:
    for line in fp:
        test_data= line.strip()
        test_data= json.loads(test_data)
        #print(1,test_data)
        with open('key2.txt',encoding='utf-8') as fp:
            for line2 in fp:
                test_step = line2.strip()
                if re.search(r'\${.*?}',test_step):
                    key= re.search(r'\${(.*?)}',test_step).group(1)
                    print(99,key)
                    test_step= re.sub(r'\${.*?}',test_data[key],test_step)
                print(2,test_step)
                if len(test_step.split('||')) == 1:
                    command = '%s()' % test_step
                elif len(test_step.split('||')) == 2:
                    command = '%s("%s")' % (test_step.split('||')[0], test_step.split('||')[1])
                elif len(test_step.split('||')) == 3:
                    command = '%s("%s","%s")' % (
                    test_step.split('||')[0], test_step.split('||')[1], test_step.split('||')[2])
                print(command)
                eval(command)
        print(1111111111111)







'''
with open('key.txt',encoding='utf-8') as fp:
    for line in fp:
        test_step= line.strip()
        print(1,test_step)
        if len(test_step.split('||'))==1:
            command= '%s()' %test_step
        elif len(test_step.split('||'))==2:
            command='%s("%s")' %(test_step.split('||')[0],test_step.split('||')[1])
        elif len(test_step.split('||'))==3:
            command='%s("%s","%s")' %(test_step.split('||')[0],test_step.split('||')[1],test_step.split('||')[2])
        print(command)
        eval(command)
'''



